package com.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author wangfengchen
 * 上传配置
 */
@Configuration
public class UploadWebConfig implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //获取文件的真实路径
    String os = System.getProperty("os.name");

    if (os.toLowerCase().startsWith("win")) {
      String path1 = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\uploads\\";
      String path2 = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\images\\";
      registry.addResourceHandler("/upload/**").
          addResourceLocations("file:" + path1);
      registry.addResourceHandler("/image/**")
          .addResourceLocations("file:" + path2);
    } else {
      //linux和mac系统 可以根据逻辑再做处理
      registry.addResourceHandler("/upload/**").
          addResourceLocations("file:" + System.getProperty("user.dir") + System.getProperty("file.separator")
              + "upload" + System.getProperty("file.separator"));

      // 先找/static/image/目录，找不到找文件目录，两个路径查找
      registry.addResourceHandler("/image/**")
          .addResourceLocations("classpath:/static/image/")
          .addResourceLocations("file:" + System.getProperty("user.dir") + System.getProperty("file.separator")
              + "image" + System.getProperty("file.separator"));
    }
    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
    registry.addResourceHandler("/static/**")
        .addResourceLocations("classpath:/static/");

  }
}